package com.ruoyi.app.controller.order.controller;

import com.ruoyi.app.controller.order.service.IShopCreditOrderService;
import com.ruoyi.app.framework.base.BaseAppController;
import com.ruoyi.app.framework.manager.AsyncManager;
import com.ruoyi.app.framework.manager.factory.ApiAsyncFactory;
import com.ruoyi.common.annotation.AuthMember;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.entity.DO.member.MemberDO;
import com.ruoyi.common.entity.DO.order.ShopCreditOrderDO;
import com.ruoyi.common.entity.VO.order.ShopOrderSearchVO;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/creditOrder")
@Api(value = "积分商城订单", description = "积分商城订单模块接口")
public class CreditOrderController extends BaseAppController {

    @Resource
    private IShopCreditOrderService creditOrderService;



    @ApiOperation(value = "获取我的兑换订单列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", required = false, paramType = "query"),
    })
    @GetMapping("/searchList")
    public TableDataInfo searchList(@ApiIgnore @AuthMember MemberDO member) {
        ShopOrderSearchVO shopOrderSearchVO = new ShopOrderSearchVO();
        shopOrderSearchVO.setMemberId(member.getId());
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderStatus"))) {
            shopOrderSearchVO.setOrderStatus(ServletUtils.getParameterToInt("orderStatus"));
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("start", ServletUtils.getParameterToInt("start", 1));
        params.put("limits", ServletUtils.getParameterToInt("limits", 1000));
        params.put("orderBy", ServletUtils.getParameter("orderBy", "create_time desc"));
        BaseEntity pageVo = new BaseEntity();
        pageVo.setParams(params);
        startPage(pageVo);
        List<ShopCreditOrderDO> orderList = creditOrderService.searchList(shopOrderSearchVO);
        TableDataInfo returnData = getDataTable(orderList);
        HashMap<String, Object> appendData = new HashMap<>();
        appendData.put("orderCount", creditOrderService.searchOrderStatusCount(member.getId()));
        returnData.setAppendData(appendData);
        return returnData;
    }

    //
//    @ApiOperation(value = "获取订单详情", notes = "获取订单详情")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header"),
//            @ApiImplicitParam(name = "orderSn", value = "订单号", required = false, paramType = "query"),
//            @ApiImplicitParam(name = "orderId", value = "订单ID", required = false, paramType = "query"),
//    })
//    @ApiResponses({
//            @ApiResponse(code = 200, message = "操作成功！"),
//    })
//    @GetMapping("/detail")
//    public AjaxResult detail(@ApiIgnore @AuthMember MemberDO member) {
//        ShopOrderSearchVO shopOrderSearchVO = new ShopOrderSearchVO();
//        shopOrderSearchVO.setMemberId(member.getId());
//        if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderSn"))) {
//            shopOrderSearchVO.setOrderSn(ServletUtils.getParameter("orderSn"));
//        } else if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderId"))) {
//            shopOrderSearchVO.setOrderId(Long.parseLong(ServletUtils.getParameter("orderId")));
//        } else {
//            return error("订单号缺失");
//        }
//        return AjaxResult.success(shopOrderService.getDetail(shopOrderSearchVO));
//    }
//
//
    @ApiOperation(value = "创建订单", notes = "创建订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header"),
            @ApiImplicitParam(name = "productId", value = "商品ID", required = true, paramType = "body"),
    })
    @PostMapping("/create")
    public AjaxResult create(@ApiIgnore @AuthMember MemberDO member, @RequestBody ShopCreditOrderDO creditOrderDO) {
        if (null == creditOrderDO.getProductId()) {
            throw new BusinessException("请选择需要兑换的商品！");
        }
        creditOrderDO.setMemberId(member.getId());
        creditOrderDO.setShopId(member.getShopId());
        creditOrderService.create(creditOrderDO);
        return AjaxResult.success("兑换成功");
    }


    @ApiOperation(value = "获取核销码信息", notes = "核销码信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataTypeClass = String.class,paramType = "header"),
            @ApiImplicitParam(name = "orderSn", value = "订单号", required = false, dataTypeClass = String.class,paramType = "query"),
    })
    @GetMapping("/getExchangeInfo")
    public AjaxResult getExchangeInfo(@ApiIgnore @AuthMember MemberDO member) {
        if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderSn"))) {
            return AjaxResult.success(creditOrderService.getExchangeCode(member.getId(), ServletUtils.getParameter("orderSn")));
        } else {
            return error("订单号缺失");
        }
    }

//
//    @ApiOperation("取消订单")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "header"),
//            @ApiImplicitParam(name = "orderSn", value = "订单号", required = true, paramType = "query"),
//    })
//    @GetMapping("/cancel")
//    public AjaxResult cancel(@ApiIgnore @AuthMember MemberDO member) {
//        if (StringUtils.isNotEmpty(ServletUtils.getParameter("orderSn"))) {
//            shopOrderStatusService.cancel(ServletUtils.getParameter("orderSn"));
//            return success();
//        } else {
//            return error("订单号缺失");
//        }
//    }
//
}
